package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDto;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.base.SearchPara;
import com.linsir.core.BaseResponse;
import com.linsir.core.CodeEnum;
import com.linsir.lin.dto.AccountDTO;
import com.linsir.lin.dto.GridDTO;
import com.linsir.lin.dto.PositionDTO;
import com.linsir.lin.mapper.PositionMapper;
import com.linsir.lin.pojo.Organization;
import com.linsir.lin.pojo.Position;
import com.linsir.lin.pojo.PositionOrgan;
import com.linsir.lin.service.OrganizationService;
import com.linsir.lin.service.PositionOrganService;
import com.linsir.lin.service.PositionService;
import com.linsir.utils.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName:linsir-lin
 * @Package:com.linsir.lin.service
 * @Title: OrganizationServiceImpl
 * @Description: 职务接口实现
 * @Create:2021/7/10 22:04 by @author:huangqg
 * @UpdateRemark: 初始创建
 * @Version: 1.0.0
 */

@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper,Position> implements IService<Position>,PositionService{
    @Autowired
    private PositionOrganService positionOrganService;
    @Autowired
    private OrganizationService organizationService;
    /**
     * 列表
     * @param page
     * @param limit
     * @param searchParams
     * @return
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public GridDTO<PositionDTO> grid(String page,
                                     String limit,
                                     String searchParams) throws IOException, InvocationTargetException, IllegalAccessException
    {
        Map<String,String> map = SearchPara.toMap(searchParams);
        QueryWrapper<Position> wrapper=new QueryWrapper<Position>();
        wrapper.orderByDesc("id");
        if (map!=null)
        {
            wrapper.like("position",map.get("position"));
        }
        Page<Position> pageDto=new PageDto<Position>();
        pageDto.setCurrent(Long.parseLong(page));
        pageDto.setSize(Long.parseLong(limit));
        pageDto = super.baseMapper.selectPage(pageDto,wrapper);
        List<PositionDTO> positionDTOList=toDtoList(pageDto.getRecords());
        return new GridDTO<PositionDTO>("0","成功",pageDto.getTotal(),positionDTOList);
    }

    /**
     * 列表
     * @param page
     * @param limit
     * @param organId
     * @param searchParams
     * @return
     * @throws IOException
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public GridDTO<PositionDTO> gridList(String page,
                                      String limit,
                                      Integer organId,
                                      String searchParams) throws IOException, InvocationTargetException, IllegalAccessException{
        Map<String,String> map = SearchPara.toMap(searchParams);
        QueryWrapper<Position> wrapper=new QueryWrapper<Position>();
        wrapper.orderByDesc("id");
        if (map!=null)
        {
            wrapper.like("position",map.get("position"));
        }
        Organization organization= organizationService.getById(organId);
        if(organization.getPid()!=0) {
            wrapper.in("id", getPositionId(organId));
        }
        Page<Position> pageDto=new PageDto<Position>();
        pageDto.setCurrent(Long.parseLong(page));
        pageDto.setSize(Long.parseLong(limit));
        pageDto = super.baseMapper.selectPage(pageDto,wrapper);
        List<PositionDTO> positionDTOList=toDtoList(pageDto.getRecords());
        return new GridDTO<PositionDTO>("0","成功",pageDto.getTotal(),positionDTOList);
    }

    /**
     * 转化DTO
     * @param position 职务
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public PositionDTO toDTO(Position position)throws InvocationTargetException, IllegalAccessException
    {
        PositionDTO positionDTO=new PositionDTO();
        BeanUtils.copyProperties(positionDTO,position);
        return  positionDTO;
    }

    /**
     * 转化DtoList
     * @param positions 职务集合
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public List<PositionDTO> toDtoList(List<Position> positions) throws InvocationTargetException, IllegalAccessException
    {
        List<PositionDTO> positionDTOList=new ArrayList<PositionDTO>();
        for(Position position:positions)
        {
            PositionDTO positionDTO=new PositionDTO();
            positionDTO=toDTO(position);
            positionDTOList.add(positionDTO);
        }
        return positionDTOList;
    }

    /**
     * 添加或修改职务
     * @param map
     * @param organId
     * @return
     * @throws ParseException
     */
    @Override
    public BaseResponse<Boolean> addOrUpdate(Map<String, Object> map,Integer organId)throws ParseException{
        Position position=new Position();

        position.setPosition(map.get("position").toString());
        position.setDescription(map.get("description").toString());
        boolean data = saveOrUpdate(position);
        if (data)
        {
            PositionOrgan positionOrgan=new PositionOrgan();
            positionOrgan.setOrganId(organId);
            positionOrgan.setPositionId(position.getId());
            positionOrganService.addOrUpdate(positionOrgan);
            return new BaseResponse<Boolean>(CodeEnum.SUCCESS,"职务增加成功",data);
        }
        else
        {
            return new BaseResponse<Boolean>(CodeEnum.FAILED,"职务增加失败",data);
        }
    }

    /**
     * 批量删除职务
     * @param ids
     * @return
     */
    @Override
    public BaseResponse<Boolean> batchDel(String ids){
        if (removeByIds(StringUtils.strtOList(ids)))
        {
            return  new BaseResponse<Boolean>(CodeEnum.SUCCESS,"批量删除成功",true);
        }
        else
        {
            return  new BaseResponse<Boolean>(CodeEnum.FAILED,"批量删除失败",false);
        }
    }

    /**
     * 通过id 获取职务
     * @param id
     * @return
     */
    @Override
    public Position getById(Integer id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据职务ID获取
     * @param id
     * @return
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    @Override
    public PositionDTO getDTObyId(Integer id) throws InvocationTargetException, IllegalAccessException{
        return toDTO(getById(id));
    }

    /**
     * 编辑职务
     * @param map
     * @param positionId
     * @return
     */
    @Override
    public BaseResponse<Boolean> edit(Map<String, Object> map, Integer positionId) throws ParseException {
        Position position=getById(positionId);
        position.setPosition(map.get("position").toString());
        position.setDescription(map.get("description").toString());
        if (updateById(position))
        {
            PositionOrgan positionOrgan=new PositionOrgan();
            positionOrgan.setId(Integer.parseInt(map.get("positionOrganId").toString()));
            positionOrgan.setPositionId(positionId);
            positionOrgan.setOrganId(Integer.parseInt(map.get("organId").toString()));
            positionOrganService.addOrUpdate(positionOrgan);
            return  new BaseResponse<Boolean>(CodeEnum.SUCCESS,"编辑职务成功",true);
        }
        else
        {
            return  new BaseResponse<Boolean>(CodeEnum.FAILED,"编辑职务失败",false);
        }
    }

    public List<Integer> getPositionId(Integer organId){
        List<PositionOrgan> positionOrganList=positionOrganService.positionOrganList(organId);
        List<Integer> positionIds=new ArrayList<Integer>();
        if(positionOrganList.size()>0)
        {
            for(PositionOrgan positionOrgan:positionOrganList){
                Integer positionId=positionOrgan.getPositionId();
                positionIds.add(positionId);
            }
        }
        return positionIds;
    }
}
